<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %><!-- 数据库操作需要 -->        
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
		//获取参数，转换为相应的类型		 
		try {
			Class.forName("org.h2.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return;
		}
		String url="jdbc:h2:d:/CompanyCar/temp/mydbtest";
		Connection conn=null;
		PreparedStatement pstmt  =null;
		ResultSet rs = null;
		System.out.println("1");
		//数据库连接与查询过程中可能会引发sql异常
		try {
			//获取数据库连接(url,username,password)			
			conn = DriverManager.getConnection(url,"sa","");
			String sql1="SELECT * FROM car_info WHERE license_plate = ?;";
			pstmt=conn.prepareStatement(sql1);
			pstmt.setString(1,request.getParameter("license_plate"));
			System.out.println(sql1);
			rs = pstmt.executeQuery();
		    if (!rs.next()) 
		    {
		        // 如果查询结果为空，则弹出提示框
		        out.println("<script>alert('该车牌号不存在！');window.history.back();</script>");
		    }
		    else
		    {
		    String sql="insert into TrafficViolation(reporter,phone_number,accident_time,license_plate,violation_type,penalty_points,vehicle_condition,accident_location,compensation,accident_description,remarks) values(?,?,?,?,?,?,?,?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, request.getParameter("reporter"));
			pstmt.setString(2, request.getParameter("phone_number"));
			pstmt.setString(3, request.getParameter("accident_time"));
			pstmt.setString(4, request.getParameter("license_plate"));
			pstmt.setString(5, request.getParameter("violation_type"));
			pstmt.setString(6, request.getParameter("penalty_points"));
			pstmt.setString(7, request.getParameter("vehicle_condition"));
			pstmt.setString(8, request.getParameter("accident_location"));
			pstmt.setString(9, request.getParameter("compensation"));
			pstmt.setString(10, request.getParameter("accident_description"));
			pstmt.setString(11, request.getParameter("remarks"));
			//循环获取结果集当中的所有数据
			int result=pstmt.executeUpdate();
			System.out.println(result);
			response.sendRedirect("X-Driver_AccidentList.jsp");
		    }
		    rs.close();
			//关闭表达式对象
			pstmt.close();
			
			//关闭数据库			
			conn.close();			
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			//防止数据库未正常关闭
			try{
				if (conn!=null && (!conn.isClosed())){
					conn.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		// 转发到JSP页面	
%>				
</body>
</html>